#!/bin/bash
exec_name=EttaM1
scripts_dir=$(pwd)
project_dir=${scripts_dir}/..
build_dir=${project_dir}/build
deploy_dir=${project_dir}/deploy
install_dir=${project_dir}/install
logo_file=etta-m1.png


# start build
rm -rf ${build_dir}/ && mkdir ${build_dir} && cd ${build_dir}
qmake ${project_dir}/EttaM1.pro -r -spec linux-g++ "CONFIG+=release"
make -j$(nproc)

if [ $? -ne 0 ]; then exit 1; fi

# start deploy
rm -rf ${deploy_dir}/ && mkdir ${deploy_dir} && cd ${deploy_dir}
cp ${install_dir}/resources/images/${logo_file} ${deploy_dir}
cp ${build_dir}/${exec_name} ${deploy_dir}

cp -rf ${install_dir}/data/            .
cp -rf ${install_dir}/inputfile/       .
cp -rf ${install_dir}/scripts/         .
cp -rf ${install_dir}/resources/doc/   .
cp -rf ${install_dir}/resources/audio/ .
cp  -f ${project_dir}/poppler/unix/*   .

linuxdeployqt ${exec_name} -appimage -verbose=0 -no-translations -no-copy-copyright-files

mv -f  lib*.so* lib/
rm -f  AppRun default.desktop

# make tar.gz
if [ $# -ge 1 ]; then
  dist_name=$1
  dist_dir=${project_dir}/${dist_name}
  rm -rf ${dist_dir}/ && cp -r ${deploy_dir}/ ${dist_dir}/
  tar -zcvf ${dist_dir}.tar.gz ${dist_dir}/
  rm -rf ${dist_dir}/
fi
